package database.mybatis.b_xml_properties;

import database.mybatis.b_xml_properties.mapper.AreaMapper;
import database.mybatis.b_xml_properties.mapper.SysRoleMapper;
import database.mybatis.b_xml_properties.mapper.SysUserMapper;
import database.mybatis.entity.Area;
import database.mybatis.entity.SysRole;
import database.mybatis.entity.SysUser;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException {
        multipleDataSource();
    }
    /**
     * @Author WLJ
     * @Description //TODO 多数据源测试
     * @Date 16:24 2023/3/31
     * @Param []
     * @return void
     **/
    private static void multipleDataSource() throws IOException {
        // 区域表中默认数据源北京的id是11a，mmdpSys数据源中北京的id是11
        String id = "11";
        SqlSession defaultSession = getSession();
        AreaMapper areaDefaultMapper = defaultSession.getMapper(AreaMapper.class);
        System.out.println(areaDefaultMapper);
        Area defaultArea = areaDefaultMapper.findById(id);
        System.out.println("defaultArea:" + defaultArea);
        Area mmdpSysArea = getMmdpSession().getMapper(AreaMapper.class).findById(id);
        System.out.println("mmdpSysArea:" + mmdpSysArea);
    }
    /**
     * @Author WLJ
     * @Description //TODO 别名设置
     * @Date 16:24 2023/3/31
     * @Param []
     * @return void
     **/
    private static void aliasTest() throws IOException {
        SqlSession session = getSession();
        SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);
        SysUser sysUser = sysUserMapper.findById("1");
        System.out.println(sysUser);
    }
    private static void test2() throws IOException {
        SqlSession session = getSession();
        SysRoleMapper sysRoleMapper = session.getMapper(SysRoleMapper.class);
        SysRole sysRole1 = sysRoleMapper.findById("101");
        System.out.println("sysRole1:" + sysRole1);
        List<SysRole> all = sysRoleMapper.getAll();
        System.out.println("all:" + all);
    }
    /**
     * @Author WLJ
     * @Description //TODO 获取mmdpSys数据源中的session
     * @Date 17:11 2023/3/31
     * @Param []
     * @return org.apache.ibatis.session.SqlSession
     **/
    private static SqlSession getMmdpSession() throws IOException {
        return new SqlSessionFactoryBuilder().build(getInputStream(), "mmdpSys").openSession();
    }
    /**
     * @Author WLJ
     * @Description //TODO 获取默认数据源中的session
     * @Date 16:27 2023/3/31
     * @Param []
     * @return org.apache.ibatis.session.SqlSession
     **/
    private static SqlSession getSession() throws IOException {
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(getInputStream());
        return sqlSessionFactory.openSession();
    }
    
    private static InputStream getInputStream() throws IOException {
        String configFilePath = "database.mybatis/b_xml_properties/mybatis-config.xml";
        return Resources.getResourceAsStream(configFilePath);
    }
}
